﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dbproj
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //登录按钮计时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(pictureBox1.Location.X<150)
            {
                pictureBox1.Location =
    new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
            }
            else
            {
                // 如果是学生,进入留言板.
                if(comboBox1.Text=="学生")
                {
                    string sql = "select * from Student where SId = '" + textBox1.Text + "' and Password='" + textBox2.Text + "'";
                    Dao dao = new Dao();
                    IDataReader dr = dao.read(sql);
                    dr.Read();
                    string sID = dr["SId"].ToString();
                    //Form3 form3 = new Form3(sID);
                    //form3.Show();
                    Form7 form7 = new Form7(sID);
                    form7.Show();
                    this.Hide();
                }
                // 如果是老师,进入管理系统.
                else if(comboBox1.Text == "老师")
                {
                    string sql = "select * from Teacher where Id = '" + textBox1.Text + "' and PassWord='" + textBox2.Text + "'";
                    Dao dao = new Dao();
                    IDataReader dr = dao.read(sql); 
                    dr.Read();
                    string tID = dr["Id"].ToString();
                    Form5 form5= new Form5();
                    form5.Show();
                    //Form2 form2 = new Form2(tID);
                    //form2.Show(); // 显示Form2窗体
                    this.Hide(); // 隐藏Form1窗体
                    //this.Close(); // 关闭窗体
                }
                //else if(comboBox1.Text == "管理员")
                //{
                //    Form4 form4 = new Form4();
                //    form4.Show();
                //    this.Hide();
                //}
                timer1.Stop();
                //打开下一个窗体
            }
        }

        //登录事件
        private void button1_Click(object sender, EventArgs e)
        {
            if(login()) //登录成功
            {
                timer1.Start();
                // 隐藏多余控件
                textBox1.Visible = false;
                textBox2.Visible = false;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                comboBox1.Visible = false;
                button1.Visible = false;
                button2.Visible = false;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        private bool login()
        {
            if(textBox1.Text==""||textBox2.Text=="" || comboBox1.Text=="")
            {
                MessageBox.Show("输入不能为空，请检查！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return false;
            }
            if(comboBox1.Text=="学生")
            {
                string sql = "select * from Student where SId = '"+textBox1.Text+"' and Password='"+textBox2.Text+"'";
                Dao dao = new Dao();
                IDataReader dr = dao.read(sql);
                if (dr.Read())
                {
                    return true; //允许登录
                }
                else return false;
            }
            else if(comboBox1.Text=="老师")
            {
                string sql = "select * from Teacher where Id = '" + textBox1.Text + "' and Password='" + textBox2.Text + "'";
                Dao dao = new Dao();
                IDataReader dr = dao.read(sql);
                if (dr.Read())
                {
                    return true; //允许登录
                }
                else return false;
            }
            //else if(comboBox1.Text=="管理员")
            //{
            //    if(textBox1.Text=="admin" && textBox2.Text=="123456")
            //    {
            //        return true;
            //    }
            //    else return false;
            //}
            return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            comboBox1.Text = null;
        }
    }
}
